﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using SqlSugar;

namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 接龙
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SolitaireController : ControllerBase
    {
        private readonly ICoreCmsSolitaireServices _solitaireServices;
        private readonly ICoreCmsSolitaireItemsServices _solitaireItemsServices;
        private readonly ICoreCmsCartServices _cartServices;
        private readonly IHttpContextUser _user;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SolitaireController(ICoreCmsSolitaireServices solitaireServices, ICoreCmsSolitaireItemsServices solitaireItemsServices, IHttpContextUser user, ICoreCmsCartServices cartServices)
        {
            _solitaireServices = solitaireServices;
            _solitaireItemsServices = solitaireItemsServices;
            _user = user;
            _cartServices = cartServices;
        }


        /// <summary>
        /// 获取接龙列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetList([FromBody] FMPageByIntId entity)
        {
            WebApiCallBack jm = new WebApiCallBack() { status = true };
            var dt = DateTime.Now;

            var where = PredicateBuilder.True<CoreCmsSolitaire>();
            where = where.And(p => p.isShow == true && p.status == (int)GlobalEnumVars.SolitaireStatus.Open && p.startTime < dt && p.endTime > dt);
            var list = await _solitaireServices.QueryPageAsync(where, p => p.createTime, OrderByType.Desc, entity.page, entity.limit);

            jm.data = list;

            return jm;
        }

        /// <summary>
        /// 获取单个接龙详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetDetail([FromBody] FMIntId entity)
        {
            WebApiCallBack jm = new WebApiCallBack() { status = true };
            //获取接龙主数据
            var model = await _solitaireServices.QueryByClauseAsync(p => p.id == entity.id);
            var dt = DateTime.Now;
            if (model == null)
            {
                jm.status = false;
                jm.msg = "接龙数据获取失败";
                return jm;
            }
            if (dt < model.startTime)
            {
                jm.status = false;
                jm.msg = "接龙活动暂未开启";
                return jm;
            }
            if (dt > model.endTime)
            {
                jm.status = false;
                jm.msg = "接龙活动已结束";
                return jm;
            }

            //获取商品sku数据
            var items = await _solitaireItemsServices.QueryListByClauseAsync(p => p.solitaireId == model.id && p.isDelete == false, p => p.sortId, OrderByType.Desc);
            //获取购买记录
            var recordResult = await _solitaireServices.GetBuyRecord(entity.id);

            if (_user.ID > 0)
            {
                await _cartServices.DeleteAsync(p => p.type == (int)GlobalEnumVars.OrderType.Solitaire && p.userId == _user.ID);
            }

            //清空个人购物车
            jm.data = new
            {
                model,
                items,
                record = recordResult.data
            };

            return jm;
        }


    }
}
